﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ThridGroup.ERP.DTO;
using ThridGroup.ERP.ProductionSystem;

namespace ThridGroup.ERP.Web.Controllers.ProductionSystem
{
    public class CustomerController : Controller
    {
        private readonly ICustomerServices customerServices;

        public CustomerController(ICustomerServices customerServices)
        {
            this.customerServices = customerServices;
        }

        public IActionResult CustomerView()
        {
            return View();
        }
        /// <summary>
        /// 添加客户管理
        /// </summary>
        /// <param name="customer"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddCustomer([FromBody]CustomerDTO customer)
        {
            return await customerServices.AddCustomer(customer);
        }

        /// <summary>
        /// 删除客户管理
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<int> DeleteCustomer(int id)
        {
            return await customerServices.DeleteCustomer(id);
        }

        /// <summary>
        /// 批量删除客户管理
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task DelManyCustomer([FromBody]List<int> ids)
        {
             await customerServices.DelManyCustomer(ids);
        }

        /// <summary>
        /// 获取客户管理
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<CustomerDTO> QueryCustomer(int id)
        {
            return await customerServices.QueryCustomer(id);
        }

        /// <summary>
        /// 客户管理列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="customerName"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> ShowCustomer(int pageIndex = 1, int pageSize = 10, string? customerName = "")
        {
            var list=await customerServices.ShowCustomer(pageIndex, pageSize, customerName);
            var result = list.OrderByDescending(m => m.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return Json(new
            {
                code = 0,
                msg = "",
                count = list.Count,
                data = result

            });
        }

        /// <summary>
        /// 编辑客户管理
        /// </summary>
        /// <param name="customer"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> UpdateCustomer([FromBody] CustomerDTO customer)
        {
            return await customerServices.UpdateCustomer(customer);
        }


    }
}
